home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_msio.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.3 KB  |  229 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_msio.c,v 1.2 2000/09/19 19:00:24 lpd Exp $ */
  20. /*
  21.  * Streams for Windows text window
  22.  *
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  26.  */
  27.  
  28. /* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
  29. /* by Friedrich Nowak                                           */
  30.  
  31. /* Factored out from gp_mswin.c by JD 6/25/97 */
  32.  
  33. /*
  34.  * The MSVC compiler, when invoked with the /MD switch, considers that the
  35.  * dllimport qualifier on fprintf in stdio.h and the dllexport qualifier
  36.  * on the definition of fprintf in this file are incompatible.
  37.  * We use a hack (similar to the one in stdpre.h to deal with sys/types.h)
  38.  * to work around this.
  39.  */
  40. #define fprintf UNDEFINE_fprintf
  41. #include "stdio_.h"
  42. #undef fprintf
  43.  
  44. #include <stdlib.h>
  45. #include "gx.h"
  46. #include "gp.h"
  47. #include "windows_.h"
  48. #include <shellapi.h>
  49. #ifdef __WIN32__
  50. #include <winspool.h>
  51. #endif
  52. #include "gp_mswin.h"
  53. #include "gsdll.h"
  54.  
  55. #include "stream.h"
  56. #include "gxiodev.h"        /* must come after stream.h */
  57.  
  58. /* Imported from gp_msdos.c */
  59. int gp_file_is_console(P1(FILE *));
  60.  
  61.  
  62. /* ====== Substitute for stdio ====== */
  63.  
  64. /* Forward references */
  65. private void win_std_init(void);
  66. private stream_proc_process(win_std_read_process);
  67. private stream_proc_process(win_std_write_process);
  68. private stream_proc_available(win_std_available);
  69.  
  70. /* Use a pseudo IODevice to get win_stdio_init called at the right time. */
  71. /* This is bad architecture; we'll fix it later. */
  72. private iodev_proc_init(win_stdio_init);
  73. const gx_io_device gs_iodev_wstdio = {
  74.     /* The name is null to keep this from showing up as a resource. */
  75.     0, "Special",
  76.     {win_stdio_init, iodev_no_open_device,
  77.      iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  78.      iodev_no_delete_file, iodev_no_rename_file,
  79.      iodev_no_file_status, iodev_no_enumerate_files
  80.     }
  81. };
  82.  
  83. /* Do one-time initialization */
  84. private int
  85. win_stdio_init(gx_io_device * iodev, gs_memory_t * mem)
  86. {
  87.     win_std_init();        /* redefine stdin/out/err to our window routines */
  88.     return 0;
  89. }
  90.  
  91. /* Define alternate 'open' routines for our stdin/out/err streams. */
  92.  
  93. extern const gx_io_device gs_iodev_stdin;
  94. private int
  95. win_stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
  96.            gs_memory_t * mem)
  97. {
  98.     int code = gs_iodev_stdin.procs.open_device(iodev, access, ps, mem);
  99.     stream *s = *ps;
  100.  
  101.     if (code != 1)
  102.     return code;
  103.     s->procs.process = win_std_read_process;
  104.     s->procs.available = win_std_available;
  105.     s->file = NULL;
  106.     return 0;
  107. }
  108.  
  109. extern const gx_io_device gs_iodev_stdout;
  110. private int
  111. win_stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
  112.         gs_memory_t * mem)
  113. {
  114.     int code = gs_iodev_stdout.procs.open_device(iodev, access, ps, mem);
  115.     stream *s = *ps;
  116.  
  117.     if (code != 1)
  118.     return code;
  119.     s->procs.process = win_std_write_process;
  120.     s->procs.available = win_std_available;
  121.     s->file = NULL;
  122.     return 0;
  123. }
  124.  
  125. extern const gx_io_device gs_iodev_stderr;
  126. private int
  127. win_stderr_open(gx_io_device * iodev, const char *access, stream ** ps,
  128.         gs_memory_t * mem)
  129. {
  130.     int code = gs_iodev_stderr.procs.open_device(iodev, access, ps, mem);
  131.     stream *s = *ps;
  132.  
  133.     if (code != 1)
  134.     return code;
  135.     s->procs.process = win_std_write_process;
  136.     s->procs.available = win_std_available;
  137.     s->file = NULL;
  138.     return 0;
  139. }
  140.  
  141. /* Patch stdin/out/err to use our windows. */
  142. private void
  143. win_std_init(void)
  144. {
  145.     /* If stdxxx is the console, replace the 'open' routines, */
  146.     /* which haven't gotten called yet. */
  147.  
  148.     if (gp_file_is_console(gs_stdin))
  149.     gs_findiodevice((const byte *)"%stdin", 6)->procs.open_device =
  150.         win_stdin_open;
  151.  
  152.     if (gp_file_is_console(gs_stdout))
  153.     gs_findiodevice((const byte *)"%stdout", 7)->procs.open_device =
  154.         win_stdout_open;
  155.  
  156.     if (gp_file_is_console(gs_stderr))
  157.     gs_findiodevice((const byte *)"%stderr", 7)->procs.open_device =
  158.         win_stderr_open;
  159. }
  160.  
  161. private int
  162. win_std_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  163.              stream_cursor_write * pw, bool last)
  164. {
  165.     int count = pw->limit - pw->ptr;
  166.  
  167.     if (count == 0)        /* empty buffer */
  168.     return 1;
  169.  
  170. /* callback to get more input */
  171.     count = (*pgsdll_callback) (GSDLL_STDIN, pw->ptr + 1, count);
  172.     if (count == 0) {
  173.     /* EOF */
  174.     /* what should we do? */
  175.     return EOFC;
  176.     }
  177.     pw->ptr += count;
  178.     return 1;
  179. }
  180.  
  181. private int
  182. win_std_available(register stream * s, long *pl)
  183. {
  184.     *pl = -1;        // EOF, since we can't do it
  185.     return 0;        // OK
  186. }
  187.  
  188.  
  189. private int
  190. win_std_write_process(stream_state * st, stream_cursor_read * pr,
  191.               stream_cursor_write * ignore_pw, bool last)
  192. {
  193.     uint count = pr->limit - pr->ptr;
  194.  
  195.     (*pgsdll_callback) (GSDLL_STDOUT, (char *)(pr->ptr + 1), count);
  196.     pr->ptr = pr->limit;
  197.     return 0;
  198. }
  199.  
  200. /* This is used instead of the stdio version. */
  201. /* The declaration must be identical to that in <stdio.h>. */
  202. #if defined(_WIN32) && (defined(_MSC_VER) || defined(_WATCOM_))
  203. #if defined(_CRTAPI2)
  204. int _CRTAPI2
  205. fprintf(FILE * file, const char *fmt,...)
  206. #else
  207. _CRTIMP int __cdecl
  208. fprintf(FILE * file, const char *fmt,...)
  209. #endif
  210. #else
  211. int _Cdecl _FARFUNC
  212. fprintf(FILE _FAR * file, const char *fmt,...)
  213. #endif
  214. {
  215.     int count;
  216.     va_list args;
  217.  
  218.     va_start(args, fmt);
  219.     if (gp_file_is_console(file)) {
  220.     char buf[1024];
  221.  
  222.     count = vsprintf(buf, fmt, args);
  223.     (*pgsdll_callback) (GSDLL_STDOUT, buf, count);
  224.     } else
  225.     count = vfprintf(file, fmt, args);
  226.     va_end(args);
  227.     return count;
  228. }
  229.